﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KOPLibrary.DataManagement;

namespace KOPLibrary.Controls
{
    public partial class AbstractDataViewer : UserControl
    {
        public AbstractDataViewer()
        {
            InitializeComponent();
        }

        private AbstractData dataSource;
        public bool AllowMultiSelect { get { return ctlList.CheckBoxes; } set { ctlList.CheckBoxes = value; } }

        public AbstractData DataSource
        {
            get
            {
                return dataSource;
            }
            set
            {
                dataSource = value;
                if (value != null)
                {
                    ctlList.BeginUpdate();
                    try
                    {
                        CreateColumns();
                        CreateItems();
                        ctlList.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
                    }
                    finally
                    {
                        ctlList.EndUpdate();
                    }
                }
            }
        }

        private void CreateItems()
        {
            ctlList.Items.Clear();
            for (int i = 0; i < dataSource.Count;i++)
                ctlList.Items.Add(CreateItem(i));
        }

        private void CreateColumns()
        {
            ctlList.Columns.Clear();
            for (int i = 0; i < dataSource.Columns.Count; i++)
            {
                ColumnHeader header = new ColumnHeader();
                header.Text = dataSource.Columns[i];                
                ctlList.Columns.Add(header);
            }
        }

        public void AuroResizeColumns()
        {
            ctlList.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
        }

        private void grid_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private ListViewItem CreateItem(int index)
        {
            ListViewItem item = new ListViewItem() {
                Text = dataSource.Eval(index, dataSource.Columns[0]),
                Checked = dataSource[index].Selected,
            };
            for (int i = 1; i < dataSource.Columns.Count;i++)
                item.SubItems.Add(dataSource.Eval(index, dataSource.Columns[i]));
            return item;

        }

        internal void FilterData(AbstractData data)
        {
            if (AllowMultiSelect)
            {
                dataSource.RemoveAll(p => p.Selected == false);
            }
        }

        private void ctlList_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
        {
            
        }

        private void ctlList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void ctlList_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            dataSource[e.Item.Index].Selected = e.Item.Checked;
        }

        private void ctlCheckAll_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in ctlList.Items)
            {
                item.Checked = true;
            }
        }

        private void ctlUncheckAll_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in ctlList.Items)
            {
                item.Checked = false;
            }
        }
    }
}
